package com.xiyoumobile.dengtuo.flowershop;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import fragment.CommunityFragment;
import fragment.FirstLookFragment;
import fragment.HomeFragment;
import fragment.MeFragment;
import fragment.ShopCartFragment;

public class MainActivity extends FragmentActivity {

    //前台按钮
    RadioGroup radioGroup;
    RadioButton home;
    RadioButton firstlook;
    RadioButton community;
    RadioButton shopcart;
    RadioButton me;


    //保存五个Fragment
    public Fragment homeF;
    private Fragment firstlookF;
    private Fragment communityF;
    private Fragment shopcartF;
    private Fragment meF;

    private boolean hashome = false;
    private boolean hasfirstlook = false;
    private boolean hascommunity = false;
    private boolean hasshopcart = false;
    private boolean hasme = false;

    ImageButton imageButton;

    Bundle mSavedInstanceState;

    //管理Fragment
    private FragmentManager fragmentManager;
    @SuppressLint("JavascriptInterface")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSavedInstanceState = savedInstanceState;
        fragmentManager = getSupportFragmentManager();
        findView();


    }

    private void findView() {
        radioGroup = (RadioGroup) findViewById(R.id.radioG);

        home = (RadioButton) findViewById(R.id.home);
        firstlook = (RadioButton) findViewById(R.id.firstlook);
        community = (RadioButton) findViewById(R.id.community);
        shopcart = (RadioButton) findViewById(R.id.shopcart);
        me = (RadioButton) findViewById(R.id.me);
        imageButton = (ImageButton)findViewById(R.id.setting);
        //如果当前的内容为空，则三个底部Button未选择就让填充首页
        if (home.isChecked()) {

            if (null == homeF) {
                homeF = new HomeFragment();
                changedFragment(homeF);

            } else {

                changedFragment(homeF);
            }
            hashome = true;
        } else if (firstlook.isChecked()) {
            if (null == firstlookF) {
                firstlookF = new FirstLookFragment();
                changedFragment(firstlookF);

            } else {

                changedFragment(firstlookF);
            }
            hasfirstlook = true;
        } else if (community.isChecked()) {
            if (null == communityF) {
                communityF = new CommunityFragment();
                changedFragment(communityF);

            } else {

                changedFragment(communityF);
            }
            hascommunity = true;
        } else if (shopcart.isChecked()) {
            if (null == shopcartF) {
                shopcartF = new ShopCartFragment();
                changedFragment(shopcartF);

            } else {

                changedFragment(shopcartF);
            }
            hasshopcart = true;
        } else if (me.isChecked()) {
            if (null == meF) {
                meF = new MeFragment();
                changedFragment(meF);

            } else {

                changedFragment(meF);
            }
            hasme = true;
        }
        //如果三个都没选择
        else {

            if (null == homeF) {
                homeF = new HomeFragment();
                changedFragment(homeF);

            } else {

                changedFragment(homeF);
            }
            hashome = true;
            //并且设置选择
            home.setChecked(true);

        }
        radioGroup.setOnCheckedChangeListener(new RadioGroupCheckedListener());

    }

    private void changedFragment(Fragment tartgetFrament) {

        android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();

        boolean isExist = false;

        if(tartgetFrament == meF){
            imageButton.setVisibility(View.VISIBLE);
        }else {
            imageButton.setVisibility(View.GONE);
        }

        //隐藏所有的

        if (hashome) {
            transaction.hide(homeF);
            if (tartgetFrament == homeF) {
                isExist = true;
            }
        }


        if (hasfirstlook) {
            transaction.hide(firstlookF);
            if (tartgetFrament == firstlookF) {
                isExist = true;
            }

        }

        if (hasshopcart) {
            transaction.hide(shopcartF);
            if (tartgetFrament == shopcartF) {
                isExist = true;
            }
        }

        if (hascommunity) {
            transaction.hide(communityF);
            if (tartgetFrament == communityF) {

                isExist = true;
            }
        }

        if (hasme) {
            transaction.hide(meF);
            if (tartgetFrament == meF) {
                isExist = true;
            }
        }


        //如果已经存在，则进行show
        if (isExist) {
            transaction.show(tartgetFrament);
        } else {
            transaction.add(R.id.main_content, tartgetFrament);
        }
        transaction.commit();
    }

    public class RadioGroupCheckedListener implements RadioGroup.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {

                case R.id.home://首页
                    if (null == homeF) {
                        homeF = new HomeFragment();
                        changedFragment(homeF);
                    } else {

                        changedFragment(homeF);
                    }
                    hashome = true;
                    break;
                case R.id.firstlook:
                    if (null == firstlookF) {
                        firstlookF = new FirstLookFragment();
                        changedFragment(firstlookF);
                    } else {

                        changedFragment(firstlookF);
                    }
                    hasfirstlook = true;
                    break;
                case R.id.shopcart:
                    if (null == shopcartF) {
                        shopcartF = new ShopCartFragment();
                        changedFragment(shopcartF);
                    } else {

                        changedFragment(shopcartF);
                    }
                    hasshopcart = true;
                    break;
                case R.id.community:
                    if (null == communityF) {
                        communityF = new CommunityFragment();
                        changedFragment(communityF);
                    } else {

                        changedFragment(communityF);
                    }
                    hascommunity = true;
                    break;
                case R.id.me:
                    if (null == meF) {
                        meF = new MeFragment();
                        changedFragment(meF);
                    } else {

                        changedFragment(meF);
                    }
                    hasme = true;
                    break;
                default:
                    break;
            }
        }
    }

}
